package wator.view.swing.jogl;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;

import wator.model.Agent;
import wator.model.DrawingStrategy;
import wator.model.Simulation;
import wator.view.swing.enums.AvailableColors;

import com.jogamp.opengl.util.gl2.GLUT;

/**
 * Super class for all jogl drawing strategies. Beside the semantic
 * description, this class adds color support to the basic drawing strategies.
 * Furthermore, the GLProfile reference is stored directly in this super class
 * statically in order to facilitate fast updates for all strategies.
 */
abstract public class JoglDrawingStrategy implements DrawingStrategy {
	
	protected AvailableColors color;
	protected static GLAutoDrawable drawable = null;
	protected static GLU glu = null;
	protected static GLUT glut = null;
	protected static JoglVectorFactory vectorFactory = 
			new JoglVectorFactory3D();
	protected static double neighborhoodRadius = Math.sqrt(
			Simulation.getInstance().getPopulation().getNeighborhoodRadius2())
			/ 2;
	protected static boolean showNeighborhoodRadius;
	protected static final float ALPHA = 0.2f;
	
	
	/**
	 * Creates a drawing strategy with the specific color.
	 * 
	 * @param color
	 */
	public JoglDrawingStrategy(AvailableColors color) {
		super();
		this.color = color;
	}

	/**
	 * Get the color of this drawing strategy.
	 * 
	 * @return the color
	 */
	public AvailableColors getColor() {
		return color;
	}

	public static boolean isShowNeighborhoodRadius() {
		return showNeighborhoodRadius;
	}

	public static void setShowNeighborhoodRadius(boolean showNeighborhoodRadius) {
		JoglDrawingStrategy.showNeighborhoodRadius = showNeighborhoodRadius;
	}

	/**
	 * Set a new color for this strategy.
	 * 
	 * @param color the new color to use
	 */
	public void setColor(AvailableColors color) {
		this.color = color;
	}

	/* (non-Javadoc)
	 * @see wator.model.DrawingStrategy#draw(wator.model.Agent)
	 */
	@Override
	abstract public void draw(Agent agent);

	/**
	 * Set the GL2 reference for all jogl drawing strategies.
	 * 
	 * @param drawable the new GL2 reference
	 */
	public static void setDrawable(GLAutoDrawable drawable) {
		JoglDrawingStrategy.drawable = drawable;
	}
	
	/**
	 * Set the jogl vector factory for all jogl drawing strategies.
	 * 
	 * @param f the new vector factory
	 */
	public static void setJoglVectorFactory(JoglVectorFactory f) {
		JoglDrawingStrategy.vectorFactory = f;
	}
	
	public static void setGLU(GLU glu) {
		JoglDrawingStrategy.glu = glu;
	}
	
	public static void setGLUT(GLUT glut) {
		JoglDrawingStrategy.glut = glut;
	}
	
	protected float[] getColor4f() {
		switch(this.color) {
		case RED:
			return new float[]{1, 0, 0, 1};
		case GREEN:
			return new float[]{0, 1, 0, 1};
		case BLUE:
			return new float[]{0, 0, 1, 1};
		case WHITE:
			return new float[]{1, 1, 1, 1};
		case YELLOW:
			return new float[]{1, 1, 0, 1};
		case GREY:
			return new float[]{0.8f, 0.8f, 0.8f, 1f};
		default:
			return new float[]{1, 1, 1, 1};
		}
	}
	
}
